﻿using FtpTestTool.Models;
using NJA.FtpTestTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FtpTestTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        bool isConnect = false;
        private string ftpUrl = "";
        private string ftpUser = "";
        private string ftpPwd = "";
        /// <summary>
        /// ftpclient
        /// </summary>
        private ClsFtp clsFtp = null;

        private List<FtpNode> ftpNodes;

        private void BtnConnect_Click(object sender, RoutedEventArgs e)
        {
            if (isConnect)
            {
                clsFtp = null;
                isConnect = false;
                btnConnect.Content = "断开";
            }
            else
            {
                try
                {
                    ftpUrl = txtFtpUrl.Text.Trim();
                    ftpUser = txtUser.Text.Trim();
                    ftpPwd = txtPwd.Password.Trim();
                    if (string.IsNullOrEmpty(ftpUrl) || string.IsNullOrEmpty(ftpUser))
                    {
                        MessageBox.Show("ftp地址和用户名不允许为空");
                        return;
                    }

                    clsFtp = new ClsFtp(new Uri(ftpUrl), ftpUser, ftpPwd);
                    LoadFtpNodes();
                    isConnect = true;
                    btnConnect.Content = "连接";
                }
                catch (Exception ex)
                {
                    LoggerHelper.Logger.Error("", ex);
                    MessageBox.Show("" + ex.Message);
                }
            }
        }
        /// <summary>
        /// 加载ftp节点
        /// </summary>
        private void LoadFtpNodes()
        {
            dgFiles.ItemsSource = null;
            ftpNodes = null;
            var files = clsFtp.ListFilesAndDirectories();
            ftpNodes = new List<FtpNode>();
            FtpNode parentNode = new FtpNode()
            {
                Name = "..",
                IsFolder = true,
                UpdateTime = null,
                TypeName = "父级目录"
            };
            ftpNodes.Add(parentNode);
            if (files != null)
            {
                foreach (var fi in files)
                {
                    FtpNode node = new FtpNode()
                    {
                        Name = fi.Name,
                        IsFolder = fi.IsDirectory,
                        UpdateTime = fi.CreateTime,
                        TypeName = fi.IsDirectory ? "目录" : "文件"
                    };
                    if (!fi.IsDirectory)
                    {
                        if (fi.FileSize < 10240)
                        {
                            node.FileSize = fi.FileSize + "B";
                        }
                        else if (fi.FileSize < 1024 * 1024)
                        {
                            node.FileSize = ( fi.FileSize / 1024) + "K";
                        }
                        else
                        {
                            node.FileSize = (1f * fi.FileSize / 1024 / 1024).ToString("G") + "M";
                        }
                    }
                    ftpNodes.Add(node);
                }
                dgFiles.ItemsSource = ftpNodes;
            }
        }

        private void DgFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            int selIndex = dgFiles.SelectedIndex;
            if (ftpNodes != null && selIndex >= 0 && selIndex < ftpNodes.Count)
            {
                var selNode = ftpNodes[selIndex];
                try
                {
                    if (selIndex == 0)
                    {
                        clsFtp.ComeoutDirectory();
                    }
                    else
                        clsFtp.GotoDirectory(selNode.Name);
                    LoadFtpNodes();
                }
                catch (Exception ex)
                {
                    LoggerHelper.Logger.Error("", ex);
                }
            }
        }

        private void BtnCreateDir_Click(object sender, RoutedEventArgs e)
        {
            string dirName = txtDirName.Text.Trim();
            try
            {
                clsFtp.MakeMulDirectory(dirName);
                LoadFtpNodes();
            }
            catch (Exception ex)
            {
                LoggerHelper.Logger.Error("", ex);
            }
        }

        private void BtnUploadFile_Click(object sender, RoutedEventArgs e)
        {

        }


        private void miDown_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
